package com.gs.process.admin.context;

import com.gs.process.core.configuration.ProcessConfig;
import lombok.Getter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Shuai Guo
 * @description:
 * @date: 2021/9/29
 * @version: 1.0
 */
@Getter
@Service
public class ProcessContext {

    private ProcessConfig config;
    private Map<Long, String> userMapping;
    private Map<Long, String> roleMapping;
    private Map<Long, String> deptMapping;

    public String getUser(Long userId) {
        return userMapping.get(userId);
    }

    public String getRole(Long roleId) {
        return roleMapping.get(roleId);
    }

    public String getDept(Long deptId) {
        return deptMapping.get(deptId);
    }

    public void init(Map<String, List<Map<String, Object>>> dataMapping) {
        //先清理
        this.clear();
        //拿到用户集合
        List<Map<String, Object>> userList = dataMapping.get("userList");
        //初始化用户映射
        userList.forEach(map -> {
            userMapping.put(Long.parseLong(map.get(config.getUserId()).toString()), map.get(config.getUserRealName()).toString());
        });
        //拿到角色集合
        List<Map<String, Object>> roleList = dataMapping.get("roleList");
        //初始化角色映射
        roleList.forEach(map -> {
            roleMapping.put(Long.parseLong(map.get(config.getRoleId()).toString()), map.get(config.getRoleName()).toString());
        });
        //拿到部门集合
        List<Map<String, Object>> deptList = dataMapping.get("deptList");
        //初始化部门映射
        deptList.forEach(map -> {
            deptMapping.put(Long.parseLong(map.get(config.getDeptId()).toString()), map.get(config.getDeptName()).toString());
        });
    }

    public void clear() {
        this.userMapping = new HashMap<>();
        this.roleMapping = new HashMap<>();
        this.deptMapping = new HashMap<>();
    }

    public void setConfig(ProcessConfig processConfig) {
        this.config = processConfig;
    }
}
